package com.han.hotelplat.service.emscheduling.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.dao.emscheduling.EmschedulingDao;
import com.han.hotelplat.pojo.emscheduling.Emscheduling;
import com.han.hotelplat.service.emscheduling.EmschedulingService;

@Service
public class EmschedulingImpl implements EmschedulingService{
	@Autowired
	EmschedulingDao dao;
	@Autowired
	EntityManage entityManage;
	
	@Override
	public void saveEmscheduling(Emscheduling emscheduling) {
		dao.save(emscheduling);
	}

	@Override
	public void deleteEmschedulingByTaskId(String taskId) {
		dao.deleteByProperty("taskid", taskId);
	}

	@Override
	public void deleteEmschedulingByEmId(String emId) {
		dao.deleteByProperty("emid", emId);
	}

	@Override
	public List<Emscheduling> getEmschedulingByEmId(String emId, String startTime, String endTime) {
		String hql = "from Emscheduling where emid='" + emId
				+ "' and workdate >='" + startTime + "' and '"
				+ "' and workdate <='" + endTime + " 23:59:59'";
		return dao.findByHQL(hql);
	}

	@Override
	public void deleteEmschedulingByTaskIdAndEmId(String taskId, String emId) {
		String sql = "delete from tb_em_scheduling where taskid='" + taskId
				+ "' and emid='" + emId + "'";
		entityManage.deleteByHql(sql);
	}

	@Override
	public List<Emscheduling> getEmschedulingByTaskId(String taskId,
			String startTime, String endTime) {
		String hql = "from Emscheduling where taskId='" + taskId
				+ "' and workdate >='" + startTime + "' and '"
				+ "' and workdate <='" + endTime + " 23:59:59'";
		return dao.findByHQL(hql);
	}

	@Override
	public Map<String, Integer> getEmschedulingCount(String taskId,
			String startTime, String endTime) {
		Map<String, Integer> data = new HashMap<String, Integer>();
		String sql = "select workdate, count(emid) from tb_em_scheduling where taskid='"
				+ taskId + "'"
				+ " and workdate>='"+startTime+"'"
				+ " and workdate<='"+endTime+"' group by workdate";
		List<Object> li = entityManage.findBySql(sql.toString());
		for(int i= 0 ; i<li.size(); i++){
			String date = "";
			int count=0;
			Object[] ory = (Object[]) li.get(i);
			if(ory[0] != null){//日期
				date = DateUtil.formatYMD((Date)ory[0]);
			}
			if(ory[1] != null){//数量
				count=Integer.parseInt(ory[1].toString());
			}
			data.put(date, count);
		}
		return data;
	}

	@Override
	public List<Emscheduling> getEmschedulingByEmidAndDate(String emid,
			String date) {
		String hql = "select new Emscheduling(ec.emid, ec.taskid, ec.workdate, t.tasktype) from Emscheduling as ec, Task as t where "
				+ "t.taskid = ec.taskid and ec.emid='" + emid
				+ "' and ec.workdate >='" + date + "'";
		return dao.findByHQL(hql);
	}

	@Override
	public int getEmschedulingDateCount(String taskid, String datetime) {
		String hql = "from Emscheduling where taskid = '"+taskid+"' and workdate ='" + datetime + "'";
		return entityManage.getCountByHql(hql);
	}
}
